package mapReduce.homework.q6_orderInfoTop10;

import mapReduce.util.JobSubmit;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author:lixinlei
 * @Date:2021/4/21 12:30
 **/
public class GetMenuSailTop10App {

    public static class gmstMapper extends Mapper<LongWritable, Text,Text, NullWritable>{

        @Override
        protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
            context.write(value,NullWritable.get());
        }
    }

    public static class gmstReducer extends Reducer<Text,NullWritable,Text,NullWritable>{
        @Override
        protected void reduce(Text key, Iterable<NullWritable> values, Context context) throws IOException, InterruptedException {

            List<String> menuList = new ArrayList<String>();

            for (NullWritable value : values) {
                menuList.add(key.toString());
            }


            for (String menu : menuList.subList(0, 10)) {
                String[] fields = menu.split("\t");
                String info = fields[0]+"\t"+fields[2]+"\t"+fields[3]+"\t"+fields[4]+"\t"+fields[5]+"\t";
                context.write(new Text(info),NullWritable.get());
            }
//            System.out.println(key.toString());

        }
    }


    public static void main(String[] args) {
        JobSubmit.submitBaseJob(GetMenuSailTop10App.class,args,
                "sort",GetMenuSailTop10Comparator.class,
                "group",GetMenuSailTop10GroupComparator.class,
                GetMenuTop10Partitioner.class,20);
    }
}
